package com.childmiles.gwt.client.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Encapsulates the complicated awards data structure
 * The reason "day number" is the key for the awards Map is so 
 * that the system can be expanded to be able to add awards
 * in the future
 * 
 */
public class Awards implements Serializable {
    private Map<Integer, List<Award>> awards = new HashMap<Integer, List<Award>>(); //Day number, awards

    public Awards() {
    }
    
    public Awards(Map<Integer, List<Award>> awards) {
        this.awards = awards;
    }
    
    /**
     * 
     * @param day
     * @return list of awards or an empty list of awards
     */
    public List<Award> get(Integer day) {
        if (awards.get(day) == null) {
            ArrayList<Award> emptyAwardsList = new ArrayList<Award>();
            awards.put(day,emptyAwardsList);
            return emptyAwardsList;
        }
        else {
            return awards.get(day);
        }
    }
    
    /**
     *
     * @param awardDate (the primary key for now)
     * @return award or null if not found
     */
    public Award findByDate(Date awardDate) {
        for (Entry<Integer, List<Award>> entry : awards.entrySet())
        {
            List<Award> awardsForOneDay = entry.getValue();
            for (Award award : awardsForOneDay) {
                if (award.getAdded().equals(awardDate)) {
                    return award;
                }
             }
        }
        return null;
    }
    
    public Integer getTotalNumberOfAwards() {
        int totalNumberOfAwards = 0;
        for (Entry<Integer, List<Award>> entry : awards.entrySet()) {
            List<Award> awardsForOneDay = entry.getValue();
            totalNumberOfAwards += awardsForOneDay.size();
        }
        return totalNumberOfAwards;
    }
    
    public void add(Integer day, Award award) {
        get(day).add(award);
    }
    
    public void remove(Integer day, Award award) {
        get(day).remove(award);
    }

	public Map<Integer, List<Award>> getAwards() {
		return awards;
	}
}
